﻿<%-- 
    Copyright (C) Binod Nepal, Planet Earth Solutions Pvt. Ltd., Kathmandu.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
--%>

<%@ Page Title="Contact Us" Language="C#" MasterPageFile="~/Classifieds/Main.Master"
    AutoEventWireup="true" CodeBehind="ContactUs.aspx.cs" Inherits="MixNP.Web.Classifieds.ContactUs" %>

<asp:Content ID="Content1" ContentPlaceHolderID="StyleSheetContentPlaceHolder" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ScriptsContentPlaceHolder" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="BodyContentPlaceHolder" runat="server">
    <asp:ScriptManager runat="server" />
    <h1>
        Get In Touch</h1>
    <hr class="hr" />

    <div id="alert-panel" style="display:none;">
        <h2>
            We are glad to hear from you<i>!</i></h2>
    </div>

    <div id="contact-panel">
        <div id="placeHolder" runat="server">
        </div>
        <asp:UpdateProgress ID="updProgress" runat="server">
            <ProgressTemplate>
                <div class="ajax-container">
                    <img alt="progress" src="/spinner.gif" class="ajax-loader" />
                </div>
            </ProgressTemplate>
        </asp:UpdateProgress>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="RegenerateCaptchaLinkButton" />
            </Triggers>
            <ContentTemplate>
                <table style="width: 100%;">
                    <tbody>
                        <tr>
                            <td>
                                <label id="Label1" class="label">
                                    Human Verification:
                                </label>
                            </td>
                        </tr>
                        <tr style="height: 40px;">
                            <td>
                                <asp:Image ID="CaptchaImage" ImageUrl="~/Handlers/Captcha.ashx" runat="server" TabIndex="400" />
                                <br />
                                <asp:LinkButton ID="RegenerateCaptchaLinkButton" runat="server" Text="New Challenge"
                                    OnClick="RegenerateCaptchaLinkButton_Click" CausesValidation="false" />
                                <br />
                                <asp:TextBox ID="CaptchaTextBox" runat="server" CssClass="textbox wide" TabIndex="500" />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <div class="note">
                                    Prove that you are a human.
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Literal ID="ErrorLiteral" runat="server" />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Button ID="SendButton" runat="server" Text="Send Message" TabIndex="600" OnClick="SendButton_Click" />
                            </td>
                        </tr>
                    </tbody>
                </table>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="ScriptsContentPlaceHolderBottom"
    runat="server">
    <script type="text/javascript">
        jQuery(document).ready(function () {
            $(".ajax-container").height($(document).height() + 100);
        }
        );        
    </script>
</asp:Content>
<script runat="server">

    protected void RegenerateCaptchaLinkButton_Click(object sender, EventArgs e)
    {
        this.SetCaptchaText();
        this.UpdatePanel1.Update();
    }

    protected void SendButton_Click(object sender, EventArgs e)
    {
        string sessionCaptcha = Pes.Utility.Conversion.TryCastString(Session["Captcha"]);

        if (string.IsNullOrWhiteSpace(sessionCaptcha))
        {
            this.SetCaptchaText();
            Pes.Utility.PageUtility.RefreshPage(this.Page);
            return;
        }
        
        if (!sessionCaptcha.Equals(CaptchaTextBox.Text))
        {
            ErrorLiteral.Text = "<div class='alert'>Invalid captcha. Your email was not sent</div>";
            return;
        }

        string name = MixNP.BusinessLayer.Helpers.RequestForm.GetUserControlFormValue("YourNameTextBox", this.Page);
        string emailAddress = MixNP.BusinessLayer.Helpers.RequestForm.GetUserControlFormValue("EmailAddressTextBox", this.Page); ;
        string subject = MixNP.BusinessLayer.Helpers.RequestForm.GetUserControlFormValue("SubjectTextBox", this.Page); ;
        string message = MixNP.BusinessLayer.Helpers.RequestForm.GetUserControlFormValue("MessageTextBox", this.Page); ;
        string sendMeACopy = MixNP.BusinessLayer.Helpers.RequestForm.GetUserControlFormValue("SendMeCCCheckBox", this.Page); ;

        string recipients = string.Empty;

        recipients = ConfigurationManager.AppSettings["AdminEmailAddresses"];


        if (sendMeACopy.Equals("Yes"))
        {
            recipients += "," + emailAddress;
        }

        string emailBody = System.IO.File.ReadAllText(this.Page.Server.MapPath("~/Classifieds/Templates/NewEmail.htm"));
        emailBody = emailBody.Replace("{IPAddress}", Pes.Utility.PageUtility.GetUserIPAddress());
        emailBody = emailBody.Replace("{SenderName}", name);
        emailBody = emailBody.Replace("{SenderEmail}", emailAddress);
        emailBody = emailBody.Replace("{Subject}", subject);
        emailBody = emailBody.Replace("{Message}", message);
        emailBody = emailBody.Replace("{SendCopy}", sendMeACopy);

        if (MixNP.BusinessLayer.Email.SendEmail("MixNP.ContactUs", CaptchaTextBox.Text, recipients, subject, emailBody, "noreply@mixnp.com"))
        {
            Pes.Utility.PageUtility.ExecuteJavaScript("ContactJS", "$('#contact-panel').hide(1000);$('#alert-panel').show(1000);", this.Page);
            UpdatePanel1.Update();
        }
    }

    private void SetCaptchaText()
    {
        Random oRandom = new Random();
        int iNumber = oRandom.Next(100000, 999999);
        Session["Captcha"] = iNumber.ToString();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.SetCaptchaText();
        }

        MixNP.Web.ServiceLayer.ControlManager.AddTextBox("YourNameTextBox", "Your Name", string.Empty, 128, double.MinValue, true, string.Empty, "textbox wide", "Please type your name.", string.Empty, false, 100, this.Page, placeHolder);
        MixNP.Web.ServiceLayer.ControlManager.AddTextBox("EmailAddressTextBox", "Your Email Address", string.Empty, 128, double.MinValue, true, string.Empty, "textbox wide", "Provide your email address so that we could send you a reply.", string.Empty, false, 100, this.Page, placeHolder);
        MixNP.Web.ServiceLayer.ControlManager.AddTextBox("SubjectTextBox", "Message Subject", string.Empty, 128, double.MinValue, true, string.Empty, "textbox wide", "Please mention why are you contacting us.", string.Empty, false, 100, this.Page, placeHolder);
        MixNP.Web.ServiceLayer.ControlManager.AddTextBox("MessageTextBox", "Message", "memo (long text)", 1000, double.MinValue, true, string.Empty, "textbox wide", "Please write your message within 1000 characters.", string.Empty, false, 100, this.Page, placeHolder);
        MixNP.Web.ServiceLayer.ControlManager.AddListControl("SendMeCCCheckBox", "Send Me a Copy of This Email", "Yes,No", "No", true, string.Empty, string.Empty, false, "CheckBox", 200, this.Page, placeHolder);
    }
</script>
